package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.exportRFCPanel.exportRFCPanelSubs;

import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.News;

public class ExportNewsTable extends JTable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String[] TABLECOLUMNNAMES = { "Neu", "ID", "Grund",
			"Beschreibung" };

	private final int newWidth, idWidth, reasonWidth;

	private DefaultTableModel tableModel;

	private ArrayList<News> news;

	public ExportNewsTable() {

		this.newWidth = 40;
		this.idWidth = 40;
		this.reasonWidth = 60;

		this.news = new ArrayList<News>();

		this.setModel(this.setupTablemodel());

		this.setupColums();

	}

	public boolean isCellEditable(int x, int y) {
		return false;
	}

	private void setupColums() {

		this.getColumnModel().getColumn(0).setMaxWidth(this.newWidth);
		this.getColumnModel().getColumn(1).setMaxWidth(this.idWidth);
		this.getColumnModel().getColumn(2).setMaxWidth(this.reasonWidth);
		this.getColumnModel().getColumn(0).setMinWidth(this.newWidth - 1);
		this.getColumnModel().getColumn(1).setMinWidth(this.idWidth - 1);
		this.getColumnModel().getColumn(2).setMinWidth(this.reasonWidth - 1);

	}

	private TableModel setupTablemodel() {
		Object[][] data = null;
		this.tableModel = new DefaultTableModel(data, TABLECOLUMNNAMES);
		return tableModel;
	}

	public void clearTable() {

		while (this.tableModel.getRowCount() > 0) {
			for (int i = 0; i < this.tableModel.getRowCount(); i++) {
				this.tableModel.removeRow(i);
				this.news.remove(i);
			}
		}

	}

	public void addNewsinTable(News news) {
		this.news.add(news);
		String[] newsData = new String[4];

		if (news.isKnown()) {
			newsData[0] = "";
		} else {
			newsData[0] = "neu";
		}

		newsData[1] = news.getRfcID();
		newsData[2] = news.getTitle();
		newsData[3] = news.getMessage();

		this.tableModel.addRow(newsData);
	}

	public News getSelectedNewsinTable() {
		if (this.getSelectedRow() >= 0) {

			return this.news.get(this.getSelectedRow());
		} else {
			return null;
		}
	}

	public News getElementatRow(int i) {
		return news.get(i);
	}
}
